<?php
mysql_connect("127.0.0.1","root","sd2013");
mysql_select_db("mathRomp");

//Update user's result in the assignment
$q1=mysql_query("UPDATE `Assignment` SET `percentage`=(`results`/`numProblems`)*100 WHERE assignmentId=".$_REQUEST['assignmentId']." AND userId=".$_REQUEST['userId']."");
while($g=mysql_fetch_assoc($q1)){
  $output1[]=$g;
}
print(json_encode($output1));

//Check to see if all students have completed the assignment. If so, then set the assignment as completed
$q2=mysql_query("SELECT count(*) as total FROM `Assignment` WHERE assignmentId=".$_REQUEST['assignmentId']." AND numProblems=currentProbNum");
$a=mysql_fetch_assoc($q2);
$q3=mysql_query("SELECT numStudents FROM `Assignment-Teacher` WHERE assignmentId=".$_REQUEST['assignmentId']."");
 while($b=mysql_fetch_assoc($q3)){
      $output2=$b;
    }
$c=json_encode($output2);
$d=json_decode($c);
if($d->numStudents == $a['total']){
  $q4=mysql_query("UPDATE `Assignment-Teacher` SET completed=1 WHERE assignmentId=".$_REQUEST['assignmentId']."");
  $f=mysql_fetch_assoc($q4);
}

?>